package com.sky.myapplication.java.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.sky.myapplication.R;

//https://blog.csdn.net/guiman/article/details/51225809
public class MyViewGroupOne extends LinearLayout {
    public MyViewGroupOne(Context context) {
        super(context);
    }

    public MyViewGroupOne(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroupOne(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++){
            View children = getChildAt(i);
            measureChild(children, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int childCount = getChildCount();
        int preHeight = 0;
        for (int i = 0; i < childCount; i++){
            View children = getChildAt(i);
            int cHeight = children.getMeasuredHeight();
            if (children.getVisibility() != View.GONE){
                children.layout(1,preHeight,r,preHeight+=cHeight);
            }
        }
    }
}
